/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import freemarker.ext.beans.HashAdapter;
import java.io.IOException;
import java.math.BigInteger;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import myconfession.dao.PageDAO;
import myconfession.dto.page.Page;
import myconfession.dto.page.Pages;
import myconfession.dto.userpage.UserPage;
import myconfession.dto.userpage.UserPageList;
import myconfession.utils.ConstantProvider;
import myconfession.utils.XMLUtility;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Liem
 */
public class PageBO {

    public static void loadAllAndMarshall(String filePath) throws JAXBException {
        Pages pages = PageDAO.getPageList();
        XMLUtility.marshallJAXB(filePath, pages);
    }

    public static Page getPageById(BigInteger id) throws JAXBException {
        Pages pages = (Pages) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getPageXmlPath(), Pages.class);
        for (Page tmp : pages.getPage()) {
            if (tmp.getId().intValue() == id.intValue()) {
                return tmp;
            }
        }
        return null;
    }

    public static void addPage(Page page, String username) throws JAXBException {
        int pageId = PageDAO.createPage(page, username);
        if (pageId > 0) {
            page.setId(new BigInteger(pageId + ""));
            Pages pages = (Pages) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getPageXmlPath(), Pages.class);
            pages.getPage().add(page);
            XMLUtility.marshallJAXB(ConstantProvider.getInstance().getPageXmlPath(), pages);

            UserPage up = new UserPage();
            up.setUsername(username);
            up.setPageId(page.getId());
            up.setPagename(page.getPagename());
            up.setDescription(page.getDescription());
            up.setFbPageId(page.getFbPageId());
            up.setIsActive(page.isIsActive());
            up.setRole("owner");
            UserPageBO.addUserPage(up);
        }
    }

    public static void updatePage(Page page) throws JAXBException {

        if (PageDAO.updatePage(page)) {
            Pages pages = (Pages) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getPageXmlPath(), Pages.class);
            for (Page tmp : pages.getPage()) {
                if (tmp.getId().intValue() == page.getId().intValue()) {
                    tmp.setPagename(page.getPagename());
                    tmp.setDescription(page.getDescription());
                    tmp.setImageLink(page.getImageLink());
                    tmp.setFbPageId(page.getFbPageId());
                    tmp.setHashtag(page.getHashtag());
                    break;
                }
            }
            UserPageList userPageList = (UserPageList) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getUserpageXmlPath(), UserPageList.class);
            for (UserPage up : userPageList.getUserPage()) {
                if (up.getPageId().intValue() == page.getId().intValue()) {
                    up.setPagename(page.getPagename());
                    up.setDescription(page.getDescription());
                    up.setFbPageId(page.getFbPageId());
                    break;
                }
            }
            XMLUtility.marshallJAXB(ConstantProvider.getInstance().getPageXmlPath(), pages);
            XMLUtility.marshallJAXB(ConstantProvider.getInstance().getUserpageXmlPath(), userPageList);
        }
    }

    public static void setPageStatus(int pageId, boolean isActive)
            throws ParserConfigurationException, SAXException, IOException, TransformerConfigurationException, TransformerException {
        if (PageDAO.setActive(pageId, isActive)) {
            Document dom = XMLUtility.parseDOMfromFile(ConstantProvider.getInstance().getPageXmlPath());
            setPageStatus(dom, pageId, isActive);
            XMLUtility.writeDOMtoXml(dom, ConstantProvider.getInstance().getPageXmlPath());
            UserPageBO.setUserPageStatus(pageId, isActive);
        }
    }

    public static void setPageStatus(Node node, int pageId, boolean isActive) {
        if (node == null) {
            return;
        }
        if (node.getNodeName().equals("id")) {
            if (node.getTextContent().trim().equals(Integer.toString(pageId))) {
                Node parent = node.getParentNode();
                NodeList child = parent.getChildNodes();
                for (int i = 0; i < child.getLength(); i++) {
                    Node tmp = child.item(i);
                    if (tmp.getNodeName().equals("isActive")) {
                        if (isActive) {
                            tmp.setTextContent("true");
                        } else {
                            tmp.setTextContent("false");
                        }
                        return;
                    }
                }
            }
        }
        NodeList child = node.getChildNodes();
        for (int i = 0; i < child.getLength(); i++) {
            setPageStatus(child.item(i), pageId, isActive);
        }
    }
}
